import bookOther from './bookOther';
import bookRj from './bookRj';

const checkRj = ({ pressCode = '' }) => {
  return pressCode === 'rj' || pressCode === 'bb';
};

/**
 * 列表
 */
const bookList = async (...args) => {
  const [listRj = {}, listOther = {}] = await Promise.all([bookRj.bookList(...args), bookOther.bookList(...args)]);
  // 修复第一个版本，人教和其他出版社，教材安装状态data.json，共用的问题
  for (const key of Object.keys(listOther)) {
    if (key.length === 13) {
      delete listOther[key];
    }
  }
  return Object.assign(listRj, listOther);
};

/**
 * 安装
 * @param {*} param0
 * @param {*} dirInfo
 */
const bookInstall = (...args) => {
  if (checkRj(...args)) {
    return bookRj.bookInstall(...args);
  }
  return bookOther.bookInstall(...args);
};

/**
 * 打开
 * @param {*} param0
 */
const bookOpen = (...args) => {
  if (checkRj(...args)) {
    return bookRj.bookOpen(...args);
  }
  return bookOther.bookOpen(...args);
};

/**
 * 打开
 * @param {*} param0
 */
const bookChaptersGet = (...args) => {
  if (checkRj(...args)) {
    return bookRj.bookChaptersGet(...args);
  }
  return bookOther.bookChaptersGet(...args);
};

/**
 * 卸载
 * @param  {...any} args
 */
const bookUninstall = (...args) => {
  if (checkRj(...args)) {
    return bookRj.bookUninstall(...args);
  }
  return bookOther.bookUninstall(...args);
};

/**
 * 暂停
 * @param  {...any} args
 */
const bookDownloadCancel = (...args) => {
  if (checkRj(...args)) {
    return bookRj.bookDownloadCancel(...args);
  }
  return bookOther.bookDownloadCancel(...args);
};

export default {
  bookList,
  bookInstall,
  bookOpen,
  bookChaptersGet,
  bookUninstall,
  bookDownloadCancel,
};
